import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "../utils/auth";
// 拼接路由
const mergePath = (path) => "/member" + path;

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("@/views/index.vue"),
    meta: { title: "业务中台首页", login: false, showTabNav: true }
  },
  {
    path: "/member",
    name: "Member",
    component: () => import("@/views/index.vue"),
    meta: { title: "C端业务中台", login: false, showTabNav: true }
  },
  {
    path: mergePath("/survey"),
    name: "Survey",
    meta: { title: "问卷调查", login: false, showTitle: false },
    component: () => import("@/views/survey/index.vue")
  },
  {
    path: mergePath("/survey/list"),
    name: "SurveyList",
    meta: { title: "问卷列表", login: false, showTitle: false },
    component: () => import("@/views/survey/survey-list.vue")
  },
  {
    path: mergePath("/survey/preview"),
    name: "SurveyPreview",
    meta: { title: "问卷调查-预览", login: false, showTitle: false },
    component: () => import("@/views/survey/survey-preview.vue")
  },
  {
    path: mergePath("/survey/success"),
    name: "SurveySuccess",
    meta: { title: "提交成功", login: false, showTitle: false },
    component: () => import("@/views/survey/survey-success.vue")
  },
  {
    path: mergePath("/survey/view"),
    name: "SurveyView",
    meta: { title: "查看问卷调查", login: false, showTitle: false },
    component: () => import("@/views/survey/survey-detail.vue")
  },
  {
    path: mergePath("/comment"),
    name: "Comment",
    meta: { title: "点评插件测试", login: false, showTitle: false },
    component: () => import("@/views/comment/comment-plugin.vue")
  },
  {
    path: mergePath("/comment/h5/commentList"),
    name: "H5CommentList",
    meta: { title: "全部评论", login: false, showTitle: false },
    component: () => import("@/views/comment/comment-list.vue")
  },
  {
    path: mergePath("/comment/h5/commentDetail"),
    name: "H5CommentDetail",
    meta: { title: "评论详情", login: false, showTitle: false },
    component: () => import("@/views/comment/comment-detail.vue")
  },
  {
    path: mergePath("/comment/h5/commentReplyDetail"),
    name: "H5CommentReplyDetail",
    meta: { title: "评论回复详情", login: false, showTitle: false },
    component: () => import("@/views/comment/comment-reply-detail.vue")
  },
  {
    path: mergePath("/comment/h5/commentOn"),
    name: "H5CommentOn",
    meta: { title: "参与点评", login: false, showTitle: false },
    component: () => import("@/views/comment/comment-on.vue")
  },
  {
    path: mergePath("/vote"),
    name: "VoteHome",
    meta: { title: "投票首页", login: false, showTitle: false },
    component: () => import("@/views/vote/index.vue")
  },
  {
    path: mergePath("/vote/ranking"),
    name: "VoteRanking",
    meta: { title: "投票排行", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-ranking.vue")
  },
  {
    path: mergePath("/vote/enroll"),
    name: "VoteEnroll",
    meta: { title: "投票报名", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-enroll.vue")
  },
  {
    path: mergePath("/vote/enroll-info"),
    name: "VoteEnrollInfo",
    meta: { title: "上传信息", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-enroll-info.vue")
  },
  {
    path: mergePath("/vote/detail"),
    name: "VoteDetail",
    meta: { title: "投票介绍", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-detail.vue")
  },
  {
    path: mergePath("/vote/item-detail"),
    name: "VoteItemDetail",
    meta: { title: "候选项详情", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-item-detail.vue")
  },
  {
    path: mergePath("/vote/preview"),
    name: "VotePreview",
    meta: { title: "投票预览", login: false, showTitle: false },
    component: () => import("@/views/vote/vote-preview.vue")
  },
  {
    path: mergePath("/enroll"),
    name: "Enroll",
    meta: { title: "报名", login: false, showTitle: false },
    component: () => import("@/views/enroll/index.vue")
  },
  {
    path: mergePath("/enroll/list"),
    name: "EnrollList",
    meta: { title: "报名列表", login: false, showTitle: false },
    component: () => import("@/views/enroll/enroll-list.vue")
  },
  {
    path: mergePath("/enroll/preview"),
    name: "EnrollPreview",
    meta: { title: "报名预览", login: false, showTitle: false },
    component: () => import("@/views/enroll/enroll-preview.vue")
  },
  {
    path: mergePath("/enroll/success"),
    name: "EnrollSuccess",
    meta: { title: "报名提交", login: false, showTitle: false },
    component: () => import("@/views/enroll/enroll-success.vue")
  },
  {
    path: mergePath("/enroll/view"),
    name: "EnrollView",
    meta: { title: "报名查看", login: false, showTitle: false },
    component: () => import("@/views/enroll/enroll-detail.vue")
  },
  {
    path: mergePath("/centerMessage"),
    name: "CenterMessage",
    meta: { title: "消息通知", login: false, showTitle: false },
    component: () => import("@/views/centerMessage/index.vue")
  },
  {
    path: mergePath("/msgDetail"),
    name: "MsgDetail",
    meta: { title: "消息通知详情", login: false, showTitle: false },
    component: () => import("@/views/centerMessage/msgDetail.vue")
  },
  {
    path: mergePath("/mine"),
    name: "Mine",
    meta: { title: "个人中心", login: false, showTitle: false },
    component: () => import("@/views/center/mine/index.vue")
  },
  // {
  //   path: "/login",
  //   name: "Login",
  //   meta: { title: "登录", login: false, showTitle: false },
  //   component: () => import("@/views/center/login/index.vue")
  // },

  // {
  //   path: "/mine/info",
  //   name: "MineInfo",
  //   meta: { title: "个人信息", login: false, showTitle: true },
  //   component: () => import("@/views/center/mine/mineInfo.vue")
  // },
  {
    path: "/:catchAll(.*)",
    name: "404",
    component: () => import("@/views/404.vue")
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});
// 路由监听
router.beforeEach((to, from, next) => {
  const token = getToken();
  if (to.meta.login) {
    if (token) next();
    else next({ name: "login" });
  } else {
    if (to.name !== "login" || !token) next();
  }
  // 页签标题
  if (to.meta.title) {
    document.title = to.meta.title || "业务中台h5";
  }
});

export default router;
